<template>
  <div>
    <!-- 搜索框 -->
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入小区或地址"
        @search="onSearch"
        @cancel="$router.back()"
        class="search"
        @input="input"
      >
      </van-search>
    </form>
    <!-- 搜索结果 -->
    <van-cell
      :value="item.communityName"
      v-for="item in searchHouseList"
      :key="item.community"
      @click="communityNameFn(item)"
    />
  </div>
</template>

<script>
import { areaCommunityAPI } from '@/api'
import { debounce } from 'loadsh'
import { mapMutations } from 'vuex'
export default {
  name: 'RentSearch',
  data() {
    return {
      value: '',
      searchHouseList: []
    }
  },
  methods: {
    ...mapMutations(['SET_COMMUNITY']),
    onSearch() {
      this.areaCommunity()
    },
    // 发起搜索
    areaCommunity: debounce(async function () {
      const { body } = await areaCommunityAPI(
        this.value,
        this.$store.state.myCity.value
      )
      this.searchHouseList = body
    }, 200),
    input() {
      this.areaCommunity()
    },
    // 存储点击对象
    communityNameFn(item) {
      this.SET_COMMUNITY(item)
      this.$router.push('/rent/add')
    }
  }
}
</script>

<style scoped lang="less">
:deep(.search) {
  height: 44px;
  background-color: #f6f5f6;
  padding: unset;
  .van-cell {
    background-color: #fff;
    padding: unset;
    height: 28px;
    align-items: center;
    .van-field__left-icon {
      margin-left: 20px;
    }
  }
}
</style>
